<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/3/29
 * Time: 10:11
 */

namespace common\models\datas;


use common\components\bases\BaseModel;
use common\components\extenders\MenuManage;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

class MediasConfig extends BaseModel
{
    public static function tableName(){
        return '{{%medias_config}}';
    }

    public function rules()
    {
        return [
            [['created_at','updated_at','mediaposition_id'], 'integer'],
            [['mediaposition_id','mediaconfig_var'], 'required'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'id' => \Yii::t('common', '自动编号'),
            'mediaconfig_var' => \Yii::t('common', '图片配置'),
            'mediaposition_id' => \Yii::t('common', '图片位置'),
            'created_at' => \Yii::t('common', '创建时间'),
            'updated_at' => \Yii::t('common', '修改时间'),
            'is_delete' => \Yii::t('common','是否删除')
        ];
    }

    public function fields(){

        $fields = parent::fields();
        $fields['updated_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->updated_at);
        };
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['is_delete'] = function () {
            switch ($this->is_delete) {
                case 0:
                    return \Yii::t('common', '正常');
                case 1:
                    return \Yii::t('common', '删除');
            }
        };
        $fields['mediaposition_id'] = function () {
            return isset($this->mediasposition->mediaposition_name)?$this->mediasposition->mediaposition_name:null;
        };
        return $fields;
    }

    public function extraFields(){
        return [
            'backend_operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
            'route_value' => function(){
                return isset($this->mediasposition->posrelated_route)?$this->mediasposition->posrelated_route:null;
            }
        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
        ]);
    }

    public function getMediasposition(){
        return $this->hasOne(MediasPosition::className(), ['id' => 'mediaposition_id']);
    }
}